package com.meihuak.cloud_ordering.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meihuak.cloud_ordering.bean.OrderChild;
import com.meihuak.cloud_ordering.bean.OrderTable;
import com.meihuak.cloud_ordering.mapper.OrderMapper;
import com.meihuak.cloud_ordering.service.BusinessService;
import com.meihuak.cloud_ordering.service.CanteenService;
import com.meihuak.cloud_ordering.service.OrderChildService;
import com.meihuak.cloud_ordering.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderTable> implements OrderService {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderChildService childService;
    @Autowired
    CanteenService canteenService;
    @Autowired
    BusinessService businessService;

    /**查询*/
    @Override
    public Long getOrderIdByParam(OrderTable orderTable){
        QueryWrapper<OrderTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id",orderTable.getStuId())
                .eq("business_id",orderTable.getBusinessId())
                .eq("create_date",orderTable.getCreateDate())
                .eq("flag",orderTable.getFlag());
        List<OrderTable> list = orderService.list(queryWrapper);
        return list.size()==0?null:list.get(0).getId();
    }

    @Override
    synchronized public Long getOrderMaxKeyId() {
        QueryWrapper<OrderTable> queryWrapper = new QueryWrapper<>();
        OrderTable order = orderService.getOne(queryWrapper.orderByDesc("id").last("limit 1"));
        return order.getId();
    }

    @Override
    public int getOrderCount(){
        List<OrderTable> list = orderService.list();
        return list.size();
    }

    /**
     * 获取窗口下的所有订单
     *
     * @param id
     */
    @Override
    public List<OrderTable> getWinAllOrderByBid(int id) {
        QueryWrapper<OrderTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",id)
                .eq("flag",0)
                .orderByAsc("create_date");

        return this.list(queryWrapper);
    }

    /**
     * 获取窗口的当前未完成订单数
     *
     * @param id
     */
    @Override
    public int getWinNoFinishOrderNumBybid(int id) {
        return this.getWinAllOrderByBid(id).size();

    }

    /**
     * 根据父订单id获取商家id
     *
     * @param id
     */
    @Override
    public int getBusinessByOrderId(long id) {
        OrderTable orderTable = this.getById(id);
        return orderTable.getBusinessId();
    }


    /**
     * 获取等待时间
     *
     * @param id
     */
    @Override
    public int getOrderWaitTime(Long id) {
        int businessid = this.getBusinessByOrderId(id);
        List<OrderTable> list = this.getWinAllOrderByBid(businessid);
        int time=0;
        for(OrderTable orderTable : list){

            time+=childService.getOrderFinishTimeById(orderTable.getId());
            if (orderTable.getId().equals(id))
                break;
        }
        return time;
    }

    /**
     * 完成订单
     *
     * @param oid
     */
    @Override
    public void finishOrder(Long oid) {
        UpdateWrapper<OrderTable> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",oid)
                .set("flag",1);
        this.update(updateWrapper);
    }
}
